package com.zy.core.common.security.util;

import com.zy.project.system.user.entity.SystemUser;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * 密码生成工具
 * Package: core.common.security.util
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-9-9 11:07
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
public class PasswordHelper {

    private static String algorithmName = "md5";
    private static int hashIterations = 2;

    public static void encryptPassword(SystemUser user) {
        //用户名加盐加密
        String newPassword = new SimpleHash(algorithmName, user.getPassword(),  ByteSource.Util.bytes(user.getUsername() + user.getSalt()), hashIterations).toHex();
        user.setPassword(newPassword);

    }

    /**
     *
     * @param username
     * @param password
     * @param salt
     * @return
     */
    public static String encryptPassword(String username,String password, String salt) {
        String newSalt = username + salt;
        //用户名加盐加密
        String newPassword = new SimpleHash(algorithmName, password,  ByteSource.Util.bytes(newSalt), hashIterations).toHex();
        return newPassword;
    }
    public static void main(String[] args) {
        SystemUser user = new SystemUser();
        user.setUsername("admin1");
        user.setPassword("admin1");
        user.setSalt("admin1admin1");
        String pas = PasswordHelper.encryptPassword("admin1", "admin1", "admin1");
        System.out.println(pas);
    }


}
